from app.config import AppConfig
from app.oss.store import Store
import grpc
from concurrent import futures
from app.algorithm.algorithm import Algorithm
from protocol import recorder_pb2_grpc
from app.service.analysis import Analysis

class App(object):
            

    def __init__(self, path):
        self._appConfig = AppConfig(path)
        self.initialize()


    def initialize(self):
        
        self._store = Store(self._appConfig.ossConfig)
        self._algorithm = Algorithm(self._store, self._appConfig.ossConfig.root)
        self._start()


    def _start(self):

        try:
            
            server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

            analysis = Analysis()
            analysis.init(self._algorithm)

            recorder_pb2_grpc.add_recorderServicer_to_server(analysis, server)
            server.add_insecure_port('[::]:8888')
            server.start()

            print("server start at 8888.")
            server.wait_for_termination()

            #self._algorithm.start()

        except Exception as ex:
            raise ConnectionError("app start algorithm exception", ex)
        
        